//
//  LGInspectChartController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/6/30.
//  检查项统计图

#import "LGInspectChartController.h"
#import "LGInspectChartView.h"
#import "LGInspectHeadView.h"
#import "LGInspectChartCell.h"
@interface LGInspectChartController ()<UITableViewDelegate,UITableViewDataSource>

/**   */
@property (nonatomic , strong)LGInspectChartView *chartView;
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)NSArray *dataArray;

@end

@implementation LGInspectChartController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"历史回顾";
    self.patientid = (_patientid && _patientid.length>0)?self.patientid:@"";
    self.timeRange = (_timeRange.length && _timeRange.length>0)?self.timeRange:@"";
    [self.view addSubview:self.tableView];
    __weak typeof(self) weakSelf = self;
    self.chartView.selectItemChnaged = ^(NSString * _Nonnull code) {
        weakSelf.timeRange = code;
        [weakSelf requestData];
    };
    [self requestData];
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/patient.Followup/itemcharts" parameters:@{@"item_code":self.code,@"hospital_bano":self.patientNum,@"patient_id":self.patientid,@"time_range":self.timeRange} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            self.chartView.chartDic = returnData[@"charts"];
        }else{
            self.chartView.chartDic = @{};
            [self.chartView.emptyView showViewWithImage:@"" content:@"数据格式不支持生成趋势图" offset:0];
        }
    } error:^(NSError *error) {
        self.chartView.chartDic = @{};
        [self.chartView.emptyView showViewWithImage:@"" content:@"数据格式不支持生成趋势图" offset:0];
    }];
    
    [RequestUtil withPOST:@"/doctor/patient.Followup/itemlist" parameters:@{@"item_code":self.code,@"hospital_bano":self.patientNum,@"time_range":self.timeRange,@"patient_id":self.patientid} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            self.chartView.itemArray = returnData[@"time_range"];
            self.chartView.title = LGNSString(returnData[@"title"]);
            self.chartView.unit = LGNSString(returnData[@"unit"]);
            self.dataArray = returnData[@"lists"];
            [self.tableView reloadData];
        }
    } error:^(NSError *error) {
        
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LGInspectChartCell *chartCell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    chartCell.selectionStyle = UITableViewCellSelectionStyleNone;
    chartCell.dataDic = self.dataArray[indexPath.row];
    return chartCell;
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return viewPix(40);
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    LGInspectHeadView *headView = [[LGInspectHeadView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(40))];
    return headView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return viewPix(15);
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    LGCornerFooterView *footerView = [[LGCornerFooterView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(15))];
    footerView.bottomMargin = viewPix(10);
    return footerView;
}



-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H-topBarHeight) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGInspectChartCell class] forCellReuseIdentifier:@"cell"];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableHeaderView = self.chartView;
    }
    return _tableView;
}

-(LGInspectChartView *)chartView{
    if(!_chartView){
        _chartView = [[LGInspectChartView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(323))];
    }
    return _chartView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
